package com.bytedance.scenedemo.group.hybrid;

import com.bytedance.scene.Scene;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.ui.ResourceTable;
import com.bytedance.scene.ui.tab.DynamicPagerIndicator;
import com.bytedance.scene.ui.template.BottomNavigationViewScene;
import com.bytedance.scene.ui.template.ItemInfo;
import com.bytedance.scene.ui.template.NavigationViewScene;
import com.bytedance.scenedemo.group.viewpager.DynamicFragmentPagerAdapter;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class HybridSample extends NavigationViewScene {
    @Override
    protected LayoutScatter onGetLayoutInflater() {
        setType(1);
        return super.onGetLayoutInflater();

    }
    @Override
    protected LinkedHashMap<String, Scene> getSceneMap() {
        LinkedHashMap map = new LinkedHashMap<String, Scene>();
        map.put("0",  HybridNavigationViewScene.newInstance(0));
        map.put("1", HybridNavigationViewScene.newInstance(1));
        map.put("2", HybridNavigationViewScene.newInstance(2));
        map.put("3", HybridNavigationViewScene.newInstance(3));
        return map;
    }

    public static class HybridNavigationViewScene extends BottomNavigationViewScene {

        private HybridNavigationViewScene() {
        }

        public static Scene newInstance(int index){
            Scene navigationViewScene = new HybridNavigationViewScene();
            IntentParams intentParams = new IntentParams();
            intentParams.setParam("index",index);
            navigationViewScene.setArguments(intentParams);
            return navigationViewScene;
        }
        @Override
        public void onViewCreated(Component view, Intent savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
        }

        @Override
        protected LinkedHashMap<String, Scene> getSceneMap() {
            int index = (int) getArguments().getParam("index");
            LinkedHashMap map = new LinkedHashMap<String, Scene>();
            map.put("主页", HybridViewPagerSample.newInstance(index * 10 + 0));
            map.put("搜索", HybridViewPagerSample.newInstance(index * 10 + 1));
            map.put("通知",  HybridViewPagerSample.newInstance(index * 10 + 2));
            return map;
        }

        @Override
        protected List<ItemInfo> getTabList() {
            List<ItemInfo> list = new ArrayList<>();
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.setTabName("主页");
            itemInfo.setColor(0xff4050B5);
            itemInfo.setIcon(ResourceTable.Media_ic_home_white_24dp);
            list.add(itemInfo);

            ItemInfo itemInfo2 = new ItemInfo();
            itemInfo2.setTabName("搜索");
            itemInfo2.setColor(0xff4050B5);
            itemInfo2.setIcon(ResourceTable.Media_sousuo);
            list.add(itemInfo2);

            ItemInfo itemInfo3 = new ItemInfo();
            itemInfo3.setTabName("通知");
            itemInfo3.setColor(0xff4050B5);
            itemInfo3.setIcon(ResourceTable.Media_ic_favorite_white_24dp);
            list.add(itemInfo3);
            return list;
        }
    }

    /**
     * 这是viewPager子cence,viewPager里面放scene 暂未成功
     */
    public static class HybridViewPagerSample extends GroupScene {
        private HybridViewPagerSample() {
        }
        public static HybridViewPagerSample newInstance(int index){
            HybridViewPagerSample hybridViewPagerSample = new HybridViewPagerSample();
            IntentParams intentParams = new IntentParams();
            intentParams.setParam("index",index);
            hybridViewPagerSample.setArguments(intentParams);
            return hybridViewPagerSample;
        }
        private static final int TEXT_SIZE = 50;
        private static final int DELAY_TIME = 100;
        @Override
        public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
            ComponentContainer componentContainer = (ComponentContainer) inflater.parse(com.bytedance.scenedemo.ResourceTable.Layout_scene_viewpager_sample,container,false);
            return componentContainer;
        }
        @Override
        public void onViewCreated(Component component, Intent intent) {
            initView(component);
            super.onViewCreated(component, intent);
        }
        private void initView(Component component) {
            int index = (int) getArguments().getParam("index");
            PageSlider viewPager7 = (PageSlider) component.findComponentById(com.bytedance.scenedemo.ResourceTable.Id_view_pager7);
            DynamicPagerIndicator dynamicPagerIndicator7 =
                    (DynamicPagerIndicator) component.findComponentById(com.bytedance.scenedemo.ResourceTable.Id_dynamic_pager_indicator7);
            setViewPagerContent(viewPager7, dynamicPagerIndicator7, 4, true,index);
        }
        private void setViewPagerContent(PageSlider viewPager, DynamicPagerIndicator dynamicPagerIndicator,
                                         int index, boolean isChange, int ii) {
            List<DirectionalLayout> fragments = createFragments(index,ii);
            DynamicFragmentPagerAdapter adapter = new DynamicFragmentPagerAdapter(fragments);
            viewPager.setProvider(adapter);
            viewPager.setCurrentPage(1,false);
            dynamicPagerIndicator.setViewPager(viewPager);
            requireAbility().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    viewPager.setCurrentPage(0,false);
                }
            }, DELAY_TIME);
        }

        private List<DirectionalLayout> createFragments(int iiii, int index) {
            List<DirectionalLayout> fragments = new ArrayList<>();
            for (int subscript = 0; subscript < iiii; subscript++) {
                String info = String.valueOf(index*100+subscript);
                fragments.add(createDirectionalLayout(subscript,info));
            }
            return fragments;
        }


        private DirectionalLayout createDirectionalLayout(int subscript, String info) {
            DirectionalLayout directionalLayout = new DirectionalLayout(requireAbility());
            DirectionalLayout.LayoutConfig layoutConfig =
                    new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                            DirectionalLayout.LayoutConfig.MATCH_PARENT);
            layoutConfig.alignment = LayoutAlignment.CENTER;
            directionalLayout.setLayoutConfig(layoutConfig);
            Text textView = new Text(requireAbility());
            textView.setTextAlignment(TextAlignment.CENTER);
            textView.setTextSize(TEXT_SIZE);
            textView.setLayoutConfig(layoutConfig);

            String infoo = new StringBuilder().append("child Scene #").append(info).toString();
            textView.setText(infoo);
            textView.setBackground(ColorUtil.getMaterialColor(subscript));
            directionalLayout.addComponent(textView);
            return directionalLayout;
        }
    }
}
